﻿using System;
using System.Web;
using System.Data;
using System.Text;
using VIT.DataHelper.DataAccess;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections.Generic;

namespace VIT.Web.Modules.NewsThanks
{
    public class FeaturedNewsProvider
    {
        #region Properties
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string LanguageId { get; set; }
        public string Image { get; set; }
        public string Title { get; set; }
        public string Brief { get; set; }
        public string Contents { get; set; }
        public string Tag { get; set; }
        public string Type { get; set; }
        public int Views { get; set; }
        public int Orders { get; set; }
        public bool Published { get; set; }
        public DateTime DisplayDate { get; set; }

        SQLSupport sqlSupport = new SQLSupport();
        #endregion

        #region Pre
        public DataTable loadTop(Dictionary<string,string> param)
        {
            int Top;
            try { Top = int.Parse(param["Top"]); }
            catch { Top = -1; }

            int Cat;
            try { Cat = int.Parse(param["Category"]); }
            catch { Cat = -1; }

            string Type;
            try { Type = param["Type"]; }
            catch { Type = string.Empty; }

            if (Top > 0)
            {
                if (Cat > 0)
                {
                    if (Type.Length > 0) return GetList(Top, Cat, Type);
                    else return GetList(Top, Cat);
                }
                else if (Type.Length > 0) return GetList(Top, Type);
                else return GetList(Top);
            }
            else return null;
        }

        public DataTable GetList(int top)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select top ");
            strQuery.Append(top);
            strQuery.Append(" a.id, al.Title,Brief,Image,DisplayDate FROM Article, Article_Language, Article_Type where Id=ArticleId and LanguageID=@LanguageID");
            strQuery.Append(" and Published=1 order by Orders, DisplayDate desc");
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            return sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId).Tables[0];
        }
        public DataTable GetList(int top, string type)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select top ");
            strQuery.Append(top);
            strQuery.Append(" a.id, al.Title,Brief,Image,DisplayDate FROM Article, Article_Language, Article_Type where Id=ArticleId and LanguageID=@LanguageID");
            strQuery.Append(" and Type like @Type");
            strQuery.Append(" and Published=1 order by Orders, DisplayDate desc");
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter Type = new SqlParameter("@Type", "%" + type.Replace(" ", "%") + "%");
            return sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, Type).Tables[0];
        }
        public DataTable GetList(int top, int category)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select top ");
            strQuery.Append(top);
            strQuery.Append(" a.id, al.Title,Brief,Image,DisplayDate FROM Article a, Article_Language al where a.Id=ArticleId and al.LanguageID=@LanguageID and CategoryId = @CategoryId and Published=1 order by Orders, DisplayDate desc");
            SqlParameter CategoryId = new SqlParameter("@CategoryId", category);
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            DataTable table = sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, CategoryId).Tables[0];
            return table;
        }
        public DataTable GetList(int top, int category, string type)
        {
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("select top ");
            strQuery.Append(top);
            strQuery.Append(" a.id, al.Title,Brief,Image,DisplayDate FROM Article a, Article_Language al, Article_Type at where a.Id=ArticleId and al.LanguageID=@LanguageID");
            strQuery.Append(" and Type like @Type");
            strQuery.Append(" and CategoryId = @CategoryId");
            strQuery.Append(" and Published=1 order by Orders, DisplayDate desc");
            SqlParameter CategoryId = new SqlParameter("@CategoryId", category);
            SqlParameter LanguageId = new SqlParameter("@LanguageID", this.LanguageId);
            SqlParameter Type = new SqlParameter("@Type", "%" + type.Replace(" ", "%") + "%");
            return sqlSupport.ExecuteDataset(false, strQuery.ToString(), LanguageId, CategoryId, Type).Tables[0];
        }
        #endregion
    }
}
